package com.mmy.fta.usercenter.service.impl;

import com.mmy.fta.usercenter.api.CouponService;
import com.mmy.fta.usercenter.dto.ShipperCouponDTO;
import com.mmy.fta.usercenter.dto.ShipperInterestsDTO;
import com.mmy.fta.usercenter.entity.CouponEntity;
import com.mmy.fta.usercenter.entity.CouponTemplateEntity;
import com.mmy.fta.usercenter.repository.CouponEntityMapper;
import com.mmy.fta.usercenter.repository.CouponTemplateEntityMapper;
import com.mmy.fta.usercenter.repository.OpenidUserMapper;
import com.ymm.common.rpc.YmmResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author qingyu.meng
 * @date 2021年10月29日 5:47 下午
 * @Description
 */

@Service
@Slf4j
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponEntityMapper couponEntityMapper;

    @Autowired
    CouponTemplateEntityMapper couponTemplateEntityMapper;

    @Autowired
    OpenidUserMapper openidUserMapper;

    @Override
    public YmmResult sendCoupons(String openId, Long couponTemplateId, Integer sendAmount) {
        log.info(">>>>>>CouponService:sendCoupons,openId is {},couponTemplateId is {},sendAmount is {}",openId,couponTemplateId,sendAmount);
        CouponTemplateEntity couponTemplateEntity = couponTemplateEntityMapper.selectByPrimaryKey(couponTemplateId);
        if (Objects.isNull(couponTemplateEntity)){
            return YmmResult.failResult(000,"模板id对应的优惠券模板不存在");
        }

        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setOpenId(openId);
        couponEntity.setCouponId(couponTemplateEntity.getId().toString());
        couponEntity.setHaveAmount(sendAmount);
        couponEntity.setBeginTime(couponTemplateEntity.getUseBeginDatetime());
        couponEntity.setEndTime(couponTemplateEntity.getUseEndDatetime());
        couponEntity.setCouponAmount(couponTemplateEntity.getCouponAmount().doubleValue());
        int result = couponEntityMapper.insertSelective(couponEntity);
        if (result == 0){
            return YmmResult.failResult(000,"发放优惠券失败");
        }

        return YmmResult.succResult();
    }

    @Override
    public YmmResult useCoupon(Long couponId) {
        log.info(">>>>>>CouponService:useCoupon,couponId is {}",couponId);
        CouponEntity couponEntity = couponEntityMapper.selectByPrimaryKey(couponId);
        if (Objects.isNull(couponEntity)){
            return YmmResult.failResult(000,"该couponId对应的优惠券不存在");
        }
        CouponEntity couponEntityUpdate = new CouponEntity();
        couponEntityUpdate.setId(couponId);
        couponEntityUpdate.setIsValid(0);
        int result = couponEntityMapper.updateByPrimaryKeySelective(couponEntityUpdate);
        if (result == 0){
            return YmmResult.failResult(000,"扣减优惠券失败");
        }
        return YmmResult.succResult();
    }

    @Override
    public Double queryCouponAmount(Long couponId) {
        return couponEntityMapper.queryCouponAmountById(couponId);
    }

    @Override
    public ShipperInterestsDTO queryShipperCoupon(String shipperOpenId) {

        ShipperInterestsDTO shipperInterestsDTO = new ShipperInterestsDTO();

        List<CouponEntity> couponEntities = couponEntityMapper.queryShipperCoupon(shipperOpenId);

        if(couponEntities.size() == 0){
            List<ShipperCouponDTO> shipperCouponDTOs = new ArrayList<>();
            shipperCouponDTOs.add(new ShipperCouponDTO());
            shipperCouponDTOs.add(new ShipperCouponDTO());

            shipperInterestsDTO.setCoupons(shipperCouponDTOs);

            log.info("couponEntitie-货主-0Coupons：{}",shipperInterestsDTO);
            return shipperInterestsDTO;
        }

        if(couponEntities.size() == 1){
            Double amount = couponEntities.get(0).getCouponAmount();

            if(amount == 95.0 ){

                ShipperCouponDTO shipperCouponDTO = new ShipperCouponDTO();

                CouponEntity couponEntity = couponEntities.get(0);
                BeanUtils.copyProperties(couponEntity, shipperCouponDTO);
                shipperCouponDTO.setCouponId(couponEntity.getId().toString());


                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String endTime = dateFormat.format(couponEntity.getEndTime());
                String startTime = dateFormat.format(couponEntity.getBeginTime());

                shipperCouponDTO.setEndTimeFormat(endTime);
                shipperCouponDTO.setStartTimeFormat(startTime);

                List<ShipperCouponDTO> shipperCouponDTOs = new ArrayList<>();
                shipperCouponDTOs.add(shipperCouponDTO);
                shipperInterestsDTO.setCoupons(shipperCouponDTOs);

                shipperInterestsDTO.getCoupons().add(new ShipperCouponDTO());
                log.info("couponEntitie-货主-95：{}",shipperInterestsDTO);

                int isNormalShipper = openidUserMapper.selectShipperIsNormalMembership(shipperOpenId);
                shipperInterestsDTO.setIsMembership(isNormalShipper);

                return shipperInterestsDTO;
            }else if(amount == 85.0){
                ShipperCouponDTO shipperCouponDTO = new ShipperCouponDTO();

                CouponEntity couponEntity = couponEntities.get(0);
                BeanUtils.copyProperties(couponEntity, shipperCouponDTO);
                shipperCouponDTO.setCouponId(couponEntity.getId().toString());


                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String endTime = dateFormat.format(couponEntity.getEndTime());
                String startTime = dateFormat.format(couponEntity.getBeginTime());

                shipperCouponDTO.setEndTimeFormat(endTime);
                shipperCouponDTO.setStartTimeFormat(startTime);

                List<ShipperCouponDTO> shipperCouponDTOs = new ArrayList<>();
                shipperCouponDTOs.add(shipperCouponDTO);
                shipperInterestsDTO.setCoupons(shipperCouponDTOs);

                shipperInterestsDTO.getCoupons().add(0,new ShipperCouponDTO());

                int isNormalShipper = openidUserMapper.selectShipperIsNormalMembership(shipperOpenId);
                shipperInterestsDTO.setIsMembership(isNormalShipper);
                log.info("couponEntitie-货主-85：{}",shipperInterestsDTO);
                return shipperInterestsDTO;
            }
        }
        List<ShipperCouponDTO> shipperCouponDTOs = new ArrayList<>();
        for(CouponEntity couponEntity:couponEntities){
            ShipperCouponDTO shipperCouponDTO = new ShipperCouponDTO();

            BeanUtils.copyProperties(couponEntity, shipperCouponDTO);
            shipperCouponDTO.setCouponId(couponEntity.getId().toString());


            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String endTime = dateFormat.format(couponEntity.getEndTime());
            String startTime = dateFormat.format(couponEntity.getBeginTime());

            shipperCouponDTO.setEndTimeFormat(endTime);
            shipperCouponDTO.setStartTimeFormat(startTime);

            shipperCouponDTOs.add(shipperCouponDTO);
        }
        shipperInterestsDTO.setCoupons(shipperCouponDTOs);
        int isNormalShipper = openidUserMapper.selectShipperIsNormalMembership(shipperOpenId);
        shipperInterestsDTO.setIsMembership(isNormalShipper);

        log.info("couponEntity-货主-95-85：{}",shipperInterestsDTO);
        return shipperInterestsDTO;
    }


}
